package cn.edu.ahu.aircondition.storage.realtime.action;

import cn.edu.ahu.aircondition.storage.realtime.domain.*;
import cn.edu.ahu.aircondition.storage.realtime.repository.*;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Component
@EnableKafka
public class KafkaConsumerAction {
    private final HardwareStatusRepo hardwareStatusRepo;
    private final ProcessStatusHisRepo processStatusHisRepo;
    private final ProcessStatusCurRepo processStatusCurRepo;
    private final ProcessInfoRepo processInfoRepo;
    private final HardwareStatusCurRepo hardwareStatusCurRepo;
    private final ComputerInfoRepo computerInfoRepo;

    @Autowired
    public KafkaConsumerAction(HardwareStatusRepo hardwareStatusRepo,
                               ProcessStatusHisRepo processStatusHisRepo,
                               ProcessInfoRepo processInfoRepo,
                               ProcessStatusCurRepo processStatusCurRepo,
                               HardwareStatusCurRepo hardwareStatusCurRepo,
                               ComputerInfoRepo computerInfoRepo
                               ) {
        this.hardwareStatusRepo = hardwareStatusRepo;
        this.processStatusHisRepo = processStatusHisRepo;
        this.processInfoRepo = processInfoRepo;
        this.processStatusCurRepo = processStatusCurRepo;
        this.hardwareStatusCurRepo=hardwareStatusCurRepo;
        this.computerInfoRepo=computerInfoRepo;
    }

    /**
     * 监听硬件状态 topic
     * 1、计算机性能日志
     * 2、计算机系统日志
     */
    @KafkaListener(topics = "air-hardware-status")
    public void hardwareStatus(String message) throws Exception {
        LogCommonJson logCommonJson = JSON.parseObject(message, LogCommonJson.class);
        if (logCommonJson.getLevel_name().equals("INFO")){
            System.out.println("时间戳："+logCommonJson.getTime_stamp()+"数据："+logCommonJson.getData_info_json());
            dealComputerPerformance(logCommonJson);

        }
    }

    @KafkaListener(topics = "process-status")
    public void processStatus(String message) throws Exception{
        LogCommonJson logCommonJson = JSON.parseObject(message, LogCommonJson.class);
        System.out.println(logCommonJson.getClient_ip());
        if (logCommonJson.getLevel_name().equals("INFO")){
            System.out.println("时间戳："+logCommonJson.getTime_stamp()+"数据："+logCommonJson.getData_info_json());
            dealProcessStatus(logCommonJson);
        }
    }

    /**
     * 保存 "计算机性能数据" 到 "计算机性能表"
     */
    private void dealComputerPerformance(LogCommonJson logCommonJson){
//        System.out.println(logCommonJson.getData_info_json());
        ComputerPerformanceJson hardwareStatusJson = JSON.parseObject(logCommonJson.getData_info_json(),ComputerPerformanceJson.class);
        ComputerPerformanceHisEntity computerPerformanceHisEntity = new ComputerPerformanceHisEntity();
        //ip
        computerPerformanceHisEntity.setHost(logCommonJson.getClient_ip());
        //时间
        computerPerformanceHisEntity.setCollect_time(logCommonJson.getTime_stamp()*1000);
        //内存
        computerPerformanceHisEntity.setMem_free(hardwareStatusJson.getMem_info().getMem_free());
        computerPerformanceHisEntity.setMem_total(hardwareStatusJson.getMem_info().getMem_total());
        computerPerformanceHisEntity.setMem_used(hardwareStatusJson.getMem_info().getMem_used());
        computerPerformanceHisEntity.setMem_percent(hardwareStatusJson.getMem_info().getMem_percent());
        //硬盘
        computerPerformanceHisEntity.setDisk_json(JSON.toJSONString(hardwareStatusJson.getDisk_info()));
        Long disk_total = 0L,disk_free = 0L,disk_used = 0L;
        for (ComputerPerformanceJson.DiskInfo diskInfo:hardwareStatusJson.getDisk_info()){
            disk_total += diskInfo.getDisk_total();
            disk_free += diskInfo.getDisk_free();
            disk_used += diskInfo.getDisk_used();
        }
        computerPerformanceHisEntity.setDisk_total(disk_total);
        computerPerformanceHisEntity.setDisk_free(disk_free);
        computerPerformanceHisEntity.setDisk_used(disk_used);
        computerPerformanceHisEntity.setDisk_percent(disk_used/disk_total.floatValue()*100);
        //存储
        hardwareStatusRepo.save(computerPerformanceHisEntity);

        ComputerInfoEntity computerInfoEntity = new ComputerInfoEntity();
        computerInfoEntity = computerInfoRepo.findFirstByIp(computerPerformanceHisEntity.getHost());
        ComputerPerformanceCurEntity computerPerformanceCurEntity = new ComputerPerformanceCurEntity();

        computerPerformanceCurEntity.setId(computerInfoEntity.getId());
        computerPerformanceCurEntity.setHost(logCommonJson.getClient_ip());
        //时间
        computerPerformanceCurEntity.setCollect_time(logCommonJson.getTime_stamp()*1000);
        //内存
        computerPerformanceCurEntity.setMem_free(hardwareStatusJson.getMem_info().getMem_free());
        computerPerformanceCurEntity.setMem_total(hardwareStatusJson.getMem_info().getMem_total());
        computerPerformanceCurEntity.setMem_used(hardwareStatusJson.getMem_info().getMem_used());
        computerPerformanceCurEntity.setMem_percent(hardwareStatusJson.getMem_info().getMem_percent());
        //硬盘
        computerPerformanceCurEntity.setDisk_json(JSON.toJSONString(hardwareStatusJson.getDisk_info()));
        computerPerformanceCurEntity.setDisk_total(computerPerformanceHisEntity.getDisk_total());
        computerPerformanceCurEntity.setDisk_free(computerPerformanceHisEntity.getDisk_free());
        computerPerformanceCurEntity.setDisk_used(computerPerformanceHisEntity.getDisk_used());
        computerPerformanceCurEntity.setDisk_percent(computerPerformanceHisEntity.getDisk_percent());
        hardwareStatusCurRepo.save(computerPerformanceCurEntity);
    }

    /**
     * 保存 "计算机性能数据" 到 "计算机性能表"
     */
    private void dealProcessStatus(LogCommonJson logCommonJson){
//        System.out.println(logCommonJson.getData_info_json());
//        System.out.println("时间戳："+logCommonJson.getTime_stamp() + " IP:" + logCommonJson.getClient_ip());

        ProcessStatusJson processStatusJson = JSON.parseObject(logCommonJson.getData_info_json(),ProcessStatusJson.class);
        ProcessInfoEntity processInfoEntity = processInfoRepo.findFirstByCmdlineAndHost(processStatusJson.getProcess_cmdline().trim(),logCommonJson.getClient_ip());
//        System.out.println(logCommonJson.getClient_ip());
        // 增加历史数据
        ProcessStatusHisEntity processStatusHisEntity = new ProcessStatusHisEntity();
        //ip
        processStatusHisEntity.setProcess_info_id(processInfoEntity.getId());
//        System.out.println(processStatusHisEntity.getHost());
        //时间
        processStatusHisEntity.setCollect_time(logCommonJson.getTime_stamp()*1000);
        //内存
        processStatusHisEntity.setMem_info(processStatusJson.getProcess_memory_info());
        processStatusHisEntity.setMem_percent(processStatusJson.getProcess_memory_percent());
        //cpu
        processStatusHisEntity.setCpu_percent(processStatusJson.getProcess_cpu_percent());
        processStatusHisEntity.setCpu_time(processStatusJson.getProcess_cpu_times());
        //状态
        processStatusHisEntity.setStatus(processStatusJson.getProcess_status());
        //进程id
        processStatusHisEntity.setPid(processStatusJson.getProcess_pid());
        //存储
        processStatusHisRepo.save(processStatusHisEntity);

        // 更新状态数据
        ProcessStatusCurEntity processStatusCurEntity = new ProcessStatusCurEntity();
        processStatusCurEntity.setProcess_info_id(processInfoEntity.getId());
//        System.out.println(processStatusHisEntity.getHost());
        //时间
        processStatusCurEntity.setCollect_time(logCommonJson.getTime_stamp()*1000);
        //内存
        processStatusCurEntity.setMem_info(processStatusJson.getProcess_memory_info());
        processStatusCurEntity.setMem_percent(processStatusJson.getProcess_memory_percent());
        //cpu
        processStatusCurEntity.setCpu_percent(processStatusJson.getProcess_cpu_percent());
        processStatusCurEntity.setCpu_time(processStatusJson.getProcess_cpu_times());
        //状态
        processStatusCurEntity.setStatus(processStatusJson.getProcess_status());
        //进程id
        processStatusCurEntity.setPid(processStatusJson.getProcess_pid());
        processStatusCurRepo.save(processStatusCurEntity);
    }

}
